<?php
namespace app\admin\service;
use app\model\GoodsCate;
use app\model\Goods;
use app\model\SystemMessage;
use think\Paginator;
use think\Session;

class GoodsService{
    public function getAllCates()
    {
        $goodsCateModel = new GoodsCate;
        $cates = $goodsCateModel::where('status',1)->order('sort')->select();
        return $cates;
    }

    public function index($where,$param)
    {
        // $where = $this->indexCondition($param);
        if(!$where['where']){
            $where['where'] = '1 =1 ';
        }
        $goodsModel = new Goods;
        if($where['str']){
            $goodsList = $goodsModel::with([
                'goodsCate' => function($query){
                    $query->field('id,name');
                }
            ])->where($where['where'])->where($where['str'])->order('add_time desc,id desc')->paginate(10,false,['query'=>$param]);
        }else{
            $goodsList = $goodsModel::with([
                'goodsCate' => function($query){
                    $query->field('id,name');
                }
            ])->where($where['where'])->order('add_time desc,id desc')->paginate(10,false,['query'=>$param]);
        }
        $page = $goodsList->render();
        return ['goodsList'=>$goodsList,'page'=>$page];
    }

    //处理首页选择的条件
    public function indexCondition($param)
    {
        $where = $data = [];
        if(array_key_exists('cate_id',$param) && $param['cate_id']){
            $where['cate_id'] = $param['cate_id'];
            $data['cate_id'] = $param['cate_id'];
        }else{
            $data['cate_id'] = 0;
        }
        if(array_key_exists('title',$param) && $param['title']){
            $where['title'] = array('like','%'.$param['title'].'%');
            $data['title'] = $param['title'];
        }else{
            $data['title'] = '';
        }
        // dump($where);die;
        $str = '';
        if(array_key_exists('status',$param) && $param['status']){
            $data['status'] = $param['status'];
            $time = time();
            switch($param['status']){
                case 1:
                    $str .= 'status = 1';
                    break;
                case 2:
                    $str .= 'status = 2 AND end_time > '.$time;
                    break;
                case 3:
                    $str .= 'status = 3';
                    break;
                case 4:
                    $str .= 'status = 4 OR (status = 2 AND end_time < '.$time.')';
                    break;
                default:

            }
        }else{
            $data['status'] = 0;
        }
        return ['where'=>$where,'str'=>$str,'data'=>$data];
    }

    public function examine($post)
    {
        $id =$post['id'];
        $status = $post['status'];
        $commision = $post['commision'];
        $goodsModel = new Goods;
        $info = $goodsModel::where('id',$id)->update(['status'=>$status,'commision'=>$commision,'sh_time'=>time()]);
        if(!$info){
            return ['code'=>0,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'已审核'];
    }

    public function refuse($post)
    {
        $id = $post['id'];
        $goodsModel = new Goods;
        $goods = $goodsModel::get($id);
        if($goods->status != 1){
            return ['code'=>-1,'msg'=>'该商品现在不是审核状态'];
        }
        $arr = [
            'uid'       => $goods->uid,
            'type'      => 1,
            'admin_id'  => Session::get('uid'),
            'relate_id' => $id,
            'content'   => $post['content'],
            'add_time'  => time(),
        ];
        $systemMessageModel = new SystemMessage;
        $info_sestem_message = $systemMessageModel::insertGetId($arr);
        if(!$info_sestem_message){
            return ['code'=>-2,'msg'=>'系统繁忙'];
        }
        $info_goods = $goodsModel::where('id',$id)->update(['status'=>3,'admin_id'=>Session::get('uid'),'sh_time'=>time()]);
        if(!$info_goods){
            $systemMessageModel::where('id',$info_sestem_message)->delete();
            return ['code'=>-3,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'已拒绝'];
    }

    public function cates($param)
    {
        $goodsCateModel = new GoodsCate;
        $cates = $goodsCateModel::order('sort desc')->paginate(10,false,['query'=>$param]);
        $page = $cates->render();
        return ['cates'=>$cates,'page'=>$page];
    }

    public function addCatePost($post,$type = 1)//1：新增分类，2：编辑分类
    {
        if(!$post['name']){
            return ['code'=>0,'msg'=>'请输入分类名称'];
        }
        if(!isset($post['status'])){
            $post['status'] = 2;
        }
        $data = array(
            'name'     => $post['name'],
            'sort'     => $post['sort'],
            'status'   => $post['status'],
            'add_time' => time()
        );
        $goodsCateModel = new GoodsCate;
        if($type == 1){
            $info = $goodsCateModel->save($data);
            if(!$info){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            return ['code'=>1,'msg'=>'新增成功'];
        }else{
            unset($data['add_time']);
            $data['ud_time'] = time();
            // dump($data);die;
            $info = $goodsCateModel::where('id',$post['id'])->update($data);
            if(!$info){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            return ['code'=>1,'msg'=>'编辑成功'];
        }
    }

    public function editCateget($id)
    {
        $goodsCateModel = new GoodsCate;
        $cate = $goodsCateModel::get($id);
        return $cate;
    }

    public function editCatePost($post)
    {
        return $this->addCatePost($post,2);
    }
}
